package org.dromara.book.service.impl;

import org.dromara.book.domain.vo.SzlyBaseLocationVo;
import org.dromara.book.service.*;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.book.domain.bo.SzlyBookInventoryBo;
import org.dromara.book.domain.vo.SzlyBookInventoryVo;
import org.dromara.book.domain.SzlyBookInventory;
import org.dromara.book.mapper.SzlyBookInventoryMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 图书库存Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-19
 */
@RequiredArgsConstructor
@Service
public class SzlyBookInventoryServiceImpl implements ISzlyBookInventoryService {

    private final SzlyBookInventoryMapper baseMapper;

    /**
     * 查询图书库存
     */
    @Override
    public SzlyBookInventoryVo queryById(Long id){
        return parseVo(baseMapper.selectVoById(id));
    }

    @Override
    public List<SzlyBookInventoryVo> queryByBarCode(String barCode) {
        LambdaQueryWrapper<SzlyBookInventory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SzlyBookInventory::getBarCode,barCode);
        return baseMapper.selectVoList(wrapper);
    }

    private final ISzlyBaseIsbnService iSzlyBaseIsbnService;

    private final ISzlyBaseBatchService iSzlyBaseBatchService;

    private final ISzlyBaseLocationService iSzlyBaseLocationService;

    private final ISzlyBaseShelfService iSzlyBaseShelfService;


    private SzlyBookInventoryVo parseVo(SzlyBookInventoryVo szlyBookInventoryVo){
        szlyBookInventoryVo.setSzlyBaseLocationVo(iSzlyBaseLocationService.queryById(szlyBookInventoryVo.getLocationId()));
        szlyBookInventoryVo.setSzlyBaseShelfVo(iSzlyBaseShelfService.queryById(szlyBookInventoryVo.getShelfId()));
        szlyBookInventoryVo.setSzlyBaseBatchVo(iSzlyBaseBatchService.queryById(szlyBookInventoryVo.getBatchId()));
        szlyBookInventoryVo.setSzlyBaseIsbnVo(iSzlyBaseIsbnService.queryById(szlyBookInventoryVo.getIsbnId()));
        return szlyBookInventoryVo;
    }

    /**
     * 查询图书库存列表
     */
    @Override
    public TableDataInfo<SzlyBookInventoryVo> queryPageList(SzlyBookInventoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SzlyBookInventory> lqw = buildQueryWrapper(bo);
        Page<SzlyBookInventoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.setRecords(result.getRecords().stream().peek(i ->parseVo(i)).collect(Collectors.toList()));
        return TableDataInfo.build(result);
    }

    /**
     * 查询图书库存列表
     */
    @Override
    public List<SzlyBookInventoryVo> queryList(SzlyBookInventoryBo bo) {
        LambdaQueryWrapper<SzlyBookInventory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw).stream().peek(i->parseVo(i)).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<SzlyBookInventory> buildQueryWrapper(SzlyBookInventoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SzlyBookInventory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBarCode()), SzlyBookInventory::getBarCode, bo.getBarCode());
        lqw.eq(bo.getInTime() != null, SzlyBookInventory::getInTime, bo.getInTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SzlyBookInventory::getStatus, bo.getStatus());
        lqw.eq(bo.getLocationId() != null, SzlyBookInventory::getLocationId, bo.getLocationId());
        lqw.eq(bo.getBatchId() != null, SzlyBookInventory::getBatchId, bo.getBatchId());
        lqw.eq(bo.getPriceReal() != null, SzlyBookInventory::getPriceReal, bo.getPriceReal());
        lqw.eq(bo.getIsbnId() != null, SzlyBookInventory::getIsbnId, bo.getIsbnId());
        lqw.eq(bo.getShelfId() != null, SzlyBookInventory::getShelfId, bo.getShelfId());
        return lqw;
    }

    /**
     * 新增图书库存
     */
    @Override
    public Boolean insertByBo(SzlyBookInventoryBo bo) {
        SzlyBookInventory add = MapstructUtils.convert(bo, SzlyBookInventory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改图书库存
     */
    @Override
    public Boolean updateByBo(SzlyBookInventoryBo bo) {
        SzlyBookInventory update = MapstructUtils.convert(bo, SzlyBookInventory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SzlyBookInventory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public List<SzlyBookInventoryVo> queryByShelfIds(Collection<Long> ids) {
        if(ids.isEmpty()){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SzlyBookInventory> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SzlyBookInventory::getShelfId,ids);
        return baseMapper.selectVoList(wrapper);
    }

    /**
     * 批量删除图书库存
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
